package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.service.CheckItemService;
import constant.MessageConstant;
import entity.PageResult;
import entity.QueryPageBean;
import entity.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pojo.CheckItem;

import java.util.List;

/**
 * 检查项管理
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    //@Reference是ubbo注解，注入分布式中的远程服务对象
    // 告诉注册中心会去调用CheckItemService的服务接口，封装为对象checkItemService
    @Reference
    private CheckItemService checkItemService;
    
    /**
     * 新增检查项
     */
    @PreAuthorize("hasAnyAuthority('CHECKITEM_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        //实际开发中通过try catch来判断服务调用成功与否,进了catch说明添加失败，否则成功
        try {
            checkItemService.add(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }
    
    /**
     * 分页条件查询
     */
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/pageQuery")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = null;
        try{
            pageResult = checkItemService.pageQuery(queryPageBean);
        }catch (Exception e){
            e.printStackTrace();
        }
        return pageResult;
    }
    
    /**
     * 更新数据n
     */
    @PreAuthorize("hasAnyAuthority('CHECKITEM_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }
    
    /**
     * 删除数据
     */
    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        if (id == null || id == 0) {
            return new Result(false,"获取不到id");
        }else{
            try {
                checkItemService.deleteById(id);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
            }
        }
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
    
    /**
     * 查所有
     */
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findAll")
    public Result findAll(){
        List<CheckItem> list = checkItemService.findAll();
        if(list != null && list.size() > 0){
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }
        return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
    }
    
}
